package cn.hy.service;

import cn.hy.dto.UserDto;
import cn.hy.entity.OrderInfo;
import cn.hy.entity.OrderInfoExample;
import cn.hy.entity.SecKillOrder;
import cn.hy.mapper.OrderInfoMapper;
import cn.hy.mapper.SecKillOrderMapper;
import cn.hy.redis.key.OrderKey;
import cn.hy.vo.GoodsVo;
import cn.hy.vo.SecKillOrderVo;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static cn.hy.constants.OrderStatus.ORDER_NOT_PAY;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private SecKillOrderMapper secKillOrderMapper;

    @Override
    public SecKillOrderVo getSecKillOrderByUserIdGoodsId(Long userId, Long goodsId) {
        String json = stringRedisTemplate.opsForValue().get(OrderKey.UID_GID_ORDER.getKey(String.valueOf(userId), String.valueOf(goodsId)));
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        return JSON.parseObject(json, SecKillOrderVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createOrder(UserDto userDto, GoodsVo goodsVo) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddressId(0L);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(goodsVo.getId());
        orderInfo.setGoodsName(goodsVo.getGoodsName());
        orderInfo.setGoodsPrice(goodsVo.getSeckillPrice());
        orderInfo.setOrderChannel(1);
        orderInfo.setStatus(0);
        orderInfo.setUserId(userDto.getId());
        int i = orderInfoMapper.insert(orderInfo);
        SecKillOrder secKillOrder = new SecKillOrder();
        secKillOrder.setGoodsId(goodsVo.getId());
        secKillOrder.setOrderId(orderInfo.getId());
        secKillOrder.setUserId(userDto.getId());
        int j = secKillOrderMapper.insert(secKillOrder);
        Long userId = userDto.getId();
        Long goodsId = goodsVo.getId();
        stringRedisTemplate.opsForValue().set(OrderKey.UID_GID_ORDER.getKey(String.valueOf(userId), String.valueOf(goodsId)),
                JSON.toJSONString(secKillOrder));
        return i > 0 && j > 0;
    }


    public void closeOrder(int hour) {
        Date closeDateTime = DateUtils.addHours(new Date(), -hour);
        OrderInfoExample example = new OrderInfoExample();
        example.createCriteria()
                .andStatusEqualTo(ORDER_NOT_PAY.getCode())
                .andCreateDateLessThan(closeDateTime);
        List<OrderInfo> orderInfos = orderInfoMapper.selectByExample(example);
        for (OrderInfo orderInfo : orderInfos) {
            System.out.println("orderInfo  information " + orderInfo.getGoodsName());
        }
    }
}
